<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionController::Redirecting</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActionController::Redirecting 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/actionpack/lib/action_controller/metal/redirecting_rb.html">actionpack/lib/action_controller/metal/redirecting.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Redirecting.html#method-i-redirect_to">redirect_to</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  
    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
      
        <li>
          
            <a href="RackDelegation.html">
              ActionController::RackDelegation
            </a>
          
        </li>
      
        <li>
          
            <a href="UrlFor.html">
              ActionController::UrlFor
            </a>
          
        </li>
      
    </ul>
  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-redirect_to">
            
              <b>redirect_to</b>(options = {}, response_status = {})
            
            <a href="Redirecting.html#method-i-redirect_to" name="method-i-redirect_to" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Redirects the browser to the target specified in <code>options</code>. This
parameter can take one of three forms:</p>
<ul><li>
<p><code>Hash</code> - The URL will be generated by calling url_for with the
<code>options</code>.</p>
</li><li>
<p><code>Record</code> - The URL will be generated by calling url_for with the
<code>options</code>, which will reference a named URL for that record.</p>
</li><li>
<p><code>String</code> starting with <code>protocol://</code> (like
<code>http://</code>) or a protocol relative reference (like
<code>//</code>) - Is passed straight through as the target for
redirection.</p>
</li><li>
<p><code>String</code> not containing a protocol - The current protocol and
host is prepended to the string.</p>
</li><li>
<p><code>Proc</code> - A block that will be executed in the controller's
context. Should return any option accepted by <code>redirect_to</code>.</p>
</li><li>
<p><code>:back</code> - Back to the page that issued the request. Useful for
forms that are triggered from multiple places. Short-hand for
<code>redirect_to(request.env["HTTP_REFERER"])</code></p>

<p><a href="Redirecting.html#method-i-redirect_to">#redirect_to</a> action:
"show", id: 5 <a
href="Redirecting.html#method-i-redirect_to">#redirect_to</a> post <a
href="Redirecting.html#method-i-redirect_to">#redirect_to</a> "<a
href="http://www.rubyonrails.org">www.rubyonrails.org</a>" <a
href="Redirecting.html#method-i-redirect_to">#redirect_to</a>
"/images/screenshot.jpg" <a
href="Redirecting.html#method-i-redirect_to">#redirect_to</a> articles_url
<a href="Redirecting.html#method-i-redirect_to">#redirect_to</a> :back <a
href="Redirecting.html#method-i-redirect_to">#redirect_to</a> proc {
edit_post_url(@post) }</p>
</li></ul>

<p>The redirection happens as a “302 Found” header unless otherwise specified.</p>

<pre>redirect_to post_url(@post), status: :found
redirect_to action: 'atom', status: :moved_permanently
redirect_to post_url(@post), status: 301
redirect_to action: 'atom', status: 302</pre>

<p>The status code can either be a standard <a
href="http://www.iana.org/assignments/http-status-codes">HTTP Status
code</a> as an integer, or a symbol representing the downcased, underscored
and symbolized description. Note that the status code must be a 3xx HTTP
code, or redirection will not occur.</p>

<p>If you are using XHR requests other than GET or POST and redirecting after
the request then some browsers will follow the redirect using the original
request method. This may lead to undesirable behavior such as a double
DELETE. To work around this  you can return a <code>303 See Other</code>
status code which will be followed using a GET request.</p>

<pre>redirect_to posts_url, status: :see_other
redirect_to action: 'index', status: 303</pre>

<p>It is also possible to assign a flash message as part of the redirection.
There are two special accessors for the commonly used flash names
<code>alert</code> and <code>notice</code> as well as a general purpose
<code>flash</code> bucket.</p>

<pre>redirect_to post_url(@post), alert: &quot;Watch it, mister!&quot;
redirect_to post_url(@post), status: :found, notice: &quot;Pay attention to the road&quot;
redirect_to post_url(@post), status: 301, flash: { updated_post_id: @post.id }
redirect_to { action: 'atom' }, alert: &quot;Something serious happened&quot;</pre>

<p>When using <code>redirect_to :back</code>, if there is no referrer,
ActionController::RedirectBackError will be raised. You may specify some
fallback behavior for this case by rescuing
ActionController::RedirectBackError.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-redirect_to_source')" id="l_method-i-redirect_to_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/baa240d09c09b74e9bc69c91e4b5c9fb5bca2005/actionpack/lib/action_controller/metal/redirecting.rb#L65" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-redirect_to_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_controller/metal/redirecting.rb, line 65</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">redirect_to</span>(<span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">response_status</span> = {}) <span class="ruby-comment">#:doc:</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ActionControllerError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;Cannot redirect to nil!&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">AbstractController</span><span class="ruby-operator">::</span><span class="ruby-constant">DoubleRenderError</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">response_body</span>

  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">status</span>        = <span class="ruby-identifier">_extract_redirect_to_status</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">response_status</span>)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">location</span>      = <span class="ruby-identifier">_compute_redirect_to_location</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">response_body</span> = <span class="ruby-node">&quot;&lt;html&gt;&lt;body&gt;You are being &lt;a href=\&quot;#{ERB::Util.h(location)}\&quot;&gt;redirected&lt;/a&gt;.&lt;/body&gt;&lt;/html&gt;&quot;</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    